package com.mediocre.api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class NacosClientCommonApplication {

    // 写法2
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(NacosClientCommonApplication.class, args);
    }

    @Slf4j
    @RestController
    static class TestController {

        /*
        写法1
         */
        @Autowired
        private LoadBalancerClient loadBalancerClient;

        TestController(LoadBalancerClient loadBalancerClient) {
            this.loadBalancerClient = loadBalancerClient;
        }

        @GetMapping("/test")
        public String test() {
            // 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
            ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
            String url = serviceInstance.getUri() + "/hello?name=mediocre";
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.getForObject(url, String.class);
            // returned value: Invoke: http://10.71.9.4:8601/hello?name=mediocre, return: hello mediocre
            return "Invoke: " + url + ", return: " + result;
        }

        /*
         以下是写法2：缺点，看不出来是哪个server节点提供的服务
        private final RestTemplate restTemplate2;

        @Autowired
        public TestController(RestTemplate restTemplate) {
            this.restTemplate2 = restTemplate;
        }

        @GetMapping("/test2")
        public String test2() {
            // 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
            // 以下都报错：
            // 前端报错：{"timestamp":"2021-11-17T05:15:05.006+00:00","status":500,"error":"Internal Server Error","message":"","path":"/test"}
            // 后端报错：java.lang.IllegalStateException: No instances available for 10.71.9.4
            // String url = "http://127.0.0.1:8601/hello?name=mediocre";
            // String url = "http://127.0.0.1/hello?name=mediocre";
            // String url = "http://10.71.9.4:8601/hello?name=mediocre";
            // String url = "http://10.71.9.4/hello?name=mediocre";

            // 正确，返回值：Invoke: http://alibaba-nacos-discovery-server:8601/hello?name=mediocre, return: hello mediocre
            // String url = "http://alibaba-nacos-discovery-server:8601/hello?name=mediocre";
            // 正确，返回值：Invoke: http://alibaba-nacos-discovery-server/hello?name=mediocre, return: hello mediocre
             String url = "http://alibaba-nacos-discovery-server/hello?name=mediocre";
            String result = restTemplate2.getForObject(url, String.class);
            return "Invoke: " + url + ", return: " + result;
        }
        */
    }
}
